package com.chuanghai.light;

import com.chuanghai.light.entity.DevTime;
import com.chuanghai.light.entity.LightPoint;
import com.chuanghai.light.response.CommonReturnType;
import com.chuanghai.light.service.LightPointService;
import com.chuanghai.light.service.LightTimeListService;
import com.chuanghai.light.tcp.TCPServer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class LightController {

    @Resource
    private LightPointService lightPointService;
    @Resource
    private LightTimeListService lightTimeListService;

    TCPServer tcpServer = new TCPServer();

    /**
     * 添加一个点位
     * @param lightPoint
     * @return
     */
    @PostMapping(value ="insertPoint")
    public CommonReturnType insertPoint(@RequestBody LightPoint lightPoint){
        boolean res = lightPointService.addLightPoint(lightPoint.getPointCode() ,lightPoint.getPointName(), lightPoint.getIp());
        return CommonReturnType.create(res);
    }
    /**
     * 获取所有不同的点位
     * @return
     */
    @GetMapping(value ="queryAllPoint")
    public CommonReturnType queryAllPoint() {
        return CommonReturnType.create(lightPointService.queryAllLightPoint());
    }

    /**
     * 设置指定点位灯的状态
     * @param pointCode
     * @param state
     * @return
     */
    @PostMapping(value ="setLightStatByPointCode")
    public CommonReturnType setLightStatByPointCode(String pointCode, Boolean state){
        boolean res = tcpServer.setLightState(pointCode,state);
        return CommonReturnType.create(lightPointService.changeStateByCode(pointCode,state));
    }

    /**
     * 获取指定点位灯的状态
     * @param pointCode
     * @return
     */
    @GetMapping(value ="getLightStatByPointCode")
    public CommonReturnType getLightStatByPointCode(String pointCode){
        return CommonReturnType.create(lightPointService.getStateByCode(pointCode));
    }


    /**
     * 获取指定点位灯的网络状态
     * @param pointCode
     * @return
     */
    @GetMapping(value ="getOnlineByPointCode")
    public CommonReturnType getOnlineByPointCode(String pointCode){
        return CommonReturnType.create(lightPointService.getOnlineByPointCode(pointCode));
    }


    /**
     * 设置指定点位灯的网络状态
     * @param pointCode
     * @return
     */
    @PostMapping(value ="setOnlineByPointCode")
    public CommonReturnType setOnlineByPointCode(String pointCode, boolean online){
        lightPointService.setOnlineByPointCode(pointCode, online);
        return CommonReturnType.create(null);
    }

    /**
     * 设置所有点位灯的状态
     * @param state
     * @return
     */
    @PostMapping(value ="setAllLightStat")
    public CommonReturnType setAllLightStat(Boolean state){
        boolean res = tcpServer.setAllLightState(state);
        return CommonReturnType.create(lightPointService.changeAllState(state));
    }

    /**
     * 获取所有状态
     * @return
     */
    @GetMapping(value ="getAllState")
    public CommonReturnType getAllState(){
        return CommonReturnType.create(lightPointService.getAllState());
    }
    /**
     * 查询指定点位的时间列表
     * @param pointCode
     * @return
     */
    @GetMapping(value ="getDevTimeBypointCode")
    public CommonReturnType getDevTimeBypointCode(String pointCode){
        return CommonReturnType.create(lightTimeListService.getDevTimeByPointCode(pointCode));
    }


    /**
     * 设置新控制的时间信息
     * @param state
     * @return
     */
    @PostMapping(value ="setDevTimeByPointCode")
    public CommonReturnType setDevTimeByPointCode(@RequestBody DevTime state){
        lightTimeListService.setDevTimeBypointCode(state);
        return CommonReturnType.create(null);
    }
}
